﻿package mortal.game.view.friend
{
    import Message.Game.*;
    import com.mui.controls.*;
    import com.mui.display.*;
    import com.mui.events.*;
    import com.mui.utils.*;
    import extend.language.*;
    import flash.events.*;
    import mortal.common.*;
    import mortal.common.global.*;
    import mortal.component.window.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.utils.*;
    import mortal.game.view.common.*;
    import mortal.game.view.wizard.*;
    import mortal.mvc.core.*;

    public class FriendModule extends BaseWindow
    {
        private var _friendTabData:Array;
        private var _middlePaneBg:ScaleBitmap;
        private var _roleLevel:GTextFiled;
        private var _roleCareer:GTextFiled;
        private var _roleName:GTextFiled;
        private var _vipImg:GBitmap;
        private var _roleSign:GTextInput;
        private var _tabBar:GTabarNew;
        private var _groupInfoSpr:FriendGroupInfoSpr;
        private var _groupInfoSpr2:FriendGroupInfoSpr;
        private var _roleListSpr:RoleListSpr;
        private var _findBtn:GButton;
        private var _deleteBtn:GButton;
        private var _friendSetPanel:FriendSetPanel;
        private var _friendDeletePanel:FriendDeletePanel;
        private var _currSelGroup:int;
        private var _selfSign:String;

        public function FriendModule()
        {
            this._friendTabData = Language.getArray(40003);
            this.init();
            this.layer = LayerManager.windowLayer;
            return;
        }// end function

        private function init() : void
        {
            setSize(324, 507);
            title = Language.getString(41233);
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            this._middlePaneBg = UIFactory.bg(8, 104 + 13, 313, 351, this);
            this._roleLevel = UIFactory.gTextField("", 16, 40, 47, 20, this);
            this._roleCareer = UIFactory.gTextField("", 68, 40, 43, 20, this, GlobalStyle.textFormatHuang2);
            this._roleName = UIFactory.gTextField("", 128, 40, 105, 20, this);
            this._vipImg = UIFactory.bitmap(ImagesConst.VIP_3, 251, 40, this);
            this._vipImg.visible = false;
            this._roleSign = UIFactory.gTextInput(11 - 3, 62, 312, 23, this, "GTextInput2");
            this._roleSign.maxChars = 20;
            this._roleSign.editable = false;
            this._roleLevel.text = "Lv." + Cache.instance.role.roleInfo.level;
            this._roleName.text = Cache.instance.role.playerInfo.name;
            this._roleSign.text = Language.getString(40004);
            this._tabBar = UIFactory.gTabBarNew(2, 91 - 4, this._friendTabData, 323, 507, 62, 26, this, this.tabBarChangeHandler, "TabButtonNew");
            this._groupInfoSpr = UICompomentPool.getUICompoment(FriendGroupInfoSpr);
            this._groupInfoSpr.x = 19;
            this._groupInfoSpr.y = 127 - 12;
            this._groupInfoSpr.flag = EFriendType._EFriendtypeIntimate;
            this._groupInfoSpr.updateListType(this._currSelGroup);
            this.addChild(this._groupInfoSpr);
            this._groupInfoSpr2 = UICompomentPool.getUICompoment(FriendGroupInfoSpr);
            this._groupInfoSpr2.x = 19;
            this._groupInfoSpr2.y = 135;
            this._groupInfoSpr2.flag = EFriendType._EFriendTypeNormal;
            this._groupInfoSpr2.updateListType(this._currSelGroup);
            this.addChild(this._groupInfoSpr2);
            this._groupInfoSpr.configEventListener(MouseEvent.CLICK, this.onSwitchHandler);
            this._groupInfoSpr2.configEventListener(MouseEvent.CLICK, this.onSwitchHandler);
            this._roleListSpr = UICompomentPool.getUICompoment(RoleListSpr);
            this._roleListSpr.x = 12;
            this._roleListSpr.y = 127 - 12;
            this.addChild(this._roleListSpr);
            this._findBtn = UIFactory.gButton(Language.getString(41234), 86, 466 + 13, 68, 23, this);
            this._deleteBtn = UIFactory.gButton(Language.getString(41235), 164, 466 + 13, 68, 23, this);
            this._findBtn.configEventListener(MouseEvent.CLICK, this.onClickHandler);
            this._deleteBtn.configEventListener(MouseEvent.CLICK, this.onClickHandler);
            this._friendSetPanel = UICompomentPool.getUICompoment(FriendSetPanel);
            this._friendDeletePanel = UICompomentPool.getUICompoment(FriendDeletePanel);
            this.updateSelfInfo(Cache.instance.role);
            this.initFriendList();
            this.addListeners();
            return;
        }// end function

        private function addListeners() : void
        {
            NetDispatcher.addCmdListener(ServerCommand.GetFriendList, this.getFriendListHandler);
            return;
        }// end function

        private function removeListeners() : void
        {
            NetDispatcher.removeCmdListener(ServerCommand.GetFriendList, this.getFriendListHandler);
            return;
        }// end function

        private function initFriendList() : void
        {
            this._tabBar.selectedIndex = 0;
            this.tabBarChangeHandler();
            return;
        }// end function

        public function updateSelfInfo(param1:Object) : void
        {
            var _loc_2:SRole = null;
            var _loc_3:SPlayer = null;
            var _loc_4:String = null;
            var _loc_5:String = null;
            var _loc_6:String = null;
            if (param1)
            {
                _loc_2 = (param1 as RoleCache).roleInfo;
                _loc_3 = (param1 as RoleCache).playerInfo;
                this._roleLevel.text = "Lv." + _loc_2.level;
                _loc_4 = GameDefConfig.instance.getECamp(_loc_3.camp).text;
                this._roleName.text = _loc_3.name;
                _loc_5 = GameDefConfig.instance.getECareer(Cache.instance.role.entityInfo.career).text;
                this._roleCareer.text = _loc_5;
                _loc_6 = AvatarUtil.getPlayerAvatarUrl(Cache.instance.role.entityInfo.career, _loc_3.sex);
                if (_loc_3.VIP != 0)
                {
                    this._vipImg.visible = true;
                }
                else
                {
                    this._vipImg.visible = false;
                }
                this._roleSign.text = _loc_3.signature;
                if (_loc_3.signature == "")
                {
                    this._roleSign.text = Language.getString(40004);
                }
                this._selfSign = _loc_3.signature;
            }
            return;
        }// end function

        private function tabBarChangeHandler(event:MuiEvent = null) : void
        {
            this._currSelGroup = this._tabBar.selectedIndex;
            this._groupInfoSpr.updateListType(this._currSelGroup);
            this._groupInfoSpr2.updateListType(this._currSelGroup);
            var _loc_2:* = this._roleListSpr.roleList;
            if (this._currSelGroup == 0)
            {
                this.addChild(this._groupInfoSpr2);
                _loc_2.y = 25 + 21;
                _loc_2.height = 315 - 31;
            }
            else
            {
                DisplayUtil.removeMe(this._groupInfoSpr2);
                this.closeList(0);
                _loc_2.y = 25;
                _loc_2.height = 315;
            }
            this.reqListData();
            return;
        }// end function

        override public function show(param1:int = 0, param2:int = 0) : void
        {
            super.show();
            this.reqListData();
            return;
        }// end function

        private function reqListData() : void
        {
            var _loc_1:Array = null;
            var _loc_2:int = 0;
            if (this._currSelGroup == 3)
            {
                _loc_1 = Cache.instance.friend.getCacheDataByGroup(this._currSelGroup);
                if (_loc_1.length != 0)
                {
                    this.openList(2);
                    this.updateCurrList();
                }
            }
            else
            {
                switch(this._currSelGroup)
                {
                    case 0:
                    {
                        _loc_2 = EFriendFlag._EFriendFlagFriend;
                        break;
                    }
                    case 1:
                    {
                        _loc_2 = EFriendFlag._EFriendFlagEnemy;
                        break;
                    }
                    case 2:
                    {
                        _loc_2 = EFriendFlag._EFriendFlagBlackList;
                        break;
                    }
                    default:
                    {
                        break;
                    }
                }
                GameProxy.friend.getFriendList(_loc_2);
            }
            return;
        }// end function

        private function getFriendListHandler(param1:Object = null) : void
        {
            var _loc_4:SFriendRecord = null;
            var _loc_5:Array = null;
            var _loc_2:* = param1 as Array;
            var _loc_3:int = -1;
            for each (_loc_4 in _loc_2)
            {
                
                _loc_3 = _loc_3 < 0 ? (_loc_4.flag) : (_loc_3);
            }
            Cache.instance.friend.updateList(_loc_3, _loc_2);
            this.setFriendsListDisplay();
            if (_loc_2.length != 0)
            {
                if (_loc_3 == EFriendFlag._EFriendFlagFriend)
                {
                    _loc_5 = Cache.instance.friend.getCloseFriendList();
                    this.updateCloseList(_loc_5);
                    this.setFriendsListDisplay();
                }
                else if (_loc_3 == EFriendFlag._EFriendFlagBlackList)
                {
                }
                else
                {
                    this.openList(2);
                }
                this.updateCurrList();
            }
            else
            {
                this.updateList(new Array());
                if (this.currSelGroup == 0)
                {
                    this.updateCloseList();
                    this.updatePosition(1);
                }
            }
            return;
        }// end function

        private function setFriendsListDisplay() : void
        {
            this.openList(0);
            this.closeList(1);
            if (Cache.instance.friend.friendList.length == 0)
            {
                this.updatePosition(0);
                this.closeList(0);
            }
            else if (Cache.instance.friend.closeFriendList.length == 0)
            {
                this.closeList(0);
                this.openList(1);
            }
            this.updatePosition(1);
            this.updateRoleNum(EFriendType._EFriendTypeNormal);
            this.updateRoleNum(EFriendType._EFriendtypeIntimate);
            return;
        }// end function

        public function updateList(param1:Array) : void
        {
            if (this._roleListSpr)
            {
                this._roleListSpr.updateListRoles(param1);
            }
            return;
        }// end function

        public function updateCurrList() : void
        {
            var _loc_1:Array = [];
            switch(this._currSelGroup)
            {
                case 0:
                {
                    _loc_1 = Cache.instance.friend.friendList;
                    break;
                }
                case 1:
                {
                    _loc_1 = Cache.instance.friend.foeList;
                    break;
                }
                case 2:
                {
                    _loc_1 = Cache.instance.friend.blackList;
                    break;
                }
                case 3:
                {
                    _loc_1 = Cache.instance.friend.recentList;
                    break;
                }
                default:
                {
                    break;
                }
            }
            if (this._roleListSpr)
            {
                this._roleListSpr.updateListRoles(_loc_1);
            }
            return;
        }// end function

        public function updateCloseList(param1:Array = null) : void
        {
            if (this._roleListSpr)
            {
                if (param1 != null)
                {
                    this._roleListSpr.updateCloseFriendList(param1);
                }
                else
                {
                    this._roleListSpr.updateCloseFriendList(Cache.instance.friend.closeFriendList);
                }
            }
            return;
        }// end function

        public function onClickHandler(event:MouseEvent) : void
        {
            var _loc_2:* = this._roleListSpr.roleList;
            var _loc_3:* = this._roleListSpr.closeFriendList;
            if (event.target == this._findBtn)
            {
                this._friendSetPanel.clear();
                this._friendSetPanel.show();
            }
            if (event.target == this._deleteBtn)
            {
                this._friendDeletePanel.clear();
                this._friendDeletePanel.show();
                Dispatcher.dispatchEvent(new DataEvent(EventName.FriendDeleteListReq));
            }
            return;
        }// end function

        public function onSwitchHandler(event:MouseEvent) : void
        {
            if (this._currSelGroup == 0)
            {
                if (event.target == this._groupInfoSpr.addSpr || event.target == this._groupInfoSpr.plusSpr)
                {
                    if (this._roleListSpr && this._roleListSpr.closeFriendList.visible == true)
                    {
                        this.updatePosition(0);
                        this.closeList(0);
                    }
                    else
                    {
                        this.openList(0);
                        this.closeList(1);
                        this.updatePosition(1);
                    }
                }
                if (event.target == this._groupInfoSpr2.addSpr || event.target == this._groupInfoSpr2.plusSpr)
                {
                    if (this._roleListSpr.roleList.visible == true)
                    {
                        this.closeList(1);
                    }
                    else
                    {
                        this.openList(1);
                        this.updatePosition(0);
                        this.closeList(0);
                    }
                }
            }
            else if (this._roleListSpr && this._roleListSpr.roleList.visible == true)
            {
                this.closeList(2);
            }
            else
            {
                this.openList(2);
            }
            return;
        }// end function

        public function updatePosition(param1:int) : void
        {
            var _loc_2:int = 0;
            var _loc_3:int = 0;
            var _loc_4:int = 0;
            if (this._roleListSpr && this._roleListSpr.closeFriendList.visible == true)
            {
                if (param1 == 0)
                {
                    this._groupInfoSpr2.y = 135;
                }
                else
                {
                    _loc_2 = this._roleListSpr.closeFriendList.rowHeight;
                    _loc_3 = this._roleListSpr.closeFriendList.length;
                    _loc_4 = 135 + _loc_3 * _loc_2;
                    this._groupInfoSpr2.y = _loc_4 < 429 ? (_loc_4) : (429);
                }
            }
            return;
        }// end function

        public function openList(param1:int) : void
        {
            if (!this._roleListSpr)
            {
                return;
            }
            if (param1 == 0)
            {
                if (!this._roleListSpr.closeFriendList.visible)
                {
                    this._roleListSpr.closeFriendList.visible = true;
                    this._roleListSpr.closeFriendList.mouseEnabled = true;
                    this._roleListSpr.closeFriendList.mouseChildren = true;
                    this._groupInfoSpr.switchBtnState(true);
                }
            }
            else if (param1 == 1)
            {
                if (!this._roleListSpr.roleList.visible)
                {
                    this._roleListSpr.roleList.visible = true;
                    this._roleListSpr.roleList.mouseEnabled = true;
                    this._roleListSpr.roleList.mouseChildren = true;
                    this._groupInfoSpr2.switchBtnState(true);
                }
            }
            else if (param1 == 2)
            {
                if (!this._roleListSpr.roleList.visible)
                {
                    this._roleListSpr.roleList.visible = true;
                    this._roleListSpr.roleList.mouseEnabled = true;
                    this._roleListSpr.roleList.mouseChildren = true;
                }
                this._groupInfoSpr.switchBtnState(true);
            }
            return;
        }// end function

        public function closeList(param1:int) : void
        {
            if (!this._roleListSpr)
            {
                return;
            }
            if (param1 == 0)
            {
                if (this._roleListSpr.closeFriendList.visible)
                {
                    this._roleListSpr.closeFriendList.visible = false;
                    this._roleListSpr.closeFriendList.mouseEnabled = false;
                    this._roleListSpr.closeFriendList.mouseChildren = false;
                }
                this._groupInfoSpr.switchBtnState(false);
            }
            else if (param1 == 1)
            {
                if (this._roleListSpr.roleList.visible)
                {
                    this._roleListSpr.roleList.visible = false;
                    this._roleListSpr.roleList.mouseEnabled = false;
                    this._roleListSpr.roleList.mouseChildren = false;
                }
                this._groupInfoSpr2.switchBtnState(false);
            }
            else if (param1 == 2)
            {
                if (this._roleListSpr.roleList.visible)
                {
                    this._roleListSpr.roleList.visible = false;
                    this._roleListSpr.roleList.mouseEnabled = false;
                    this._roleListSpr.roleList.mouseChildren = false;
                }
                this._groupInfoSpr.switchBtnState(false);
            }
            return;
        }// end function

        public function updateRoleNum(param1:int) : void
        {
            switch(param1)
            {
                case EFriendType._EFriendtypeIntimate:
                {
                    if (this._groupInfoSpr)
                    {
                        this._groupInfoSpr.updateRoleNum();
                    }
                    break;
                }
                case EFriendType._EFriendTypeNormal:
                {
                    if (this._groupInfoSpr2)
                    {
                        this._groupInfoSpr2.updateRoleNum();
                    }
                    break;
                }
                case 3:
                {
                    if (this._friendDeletePanel)
                    {
                        this._friendDeletePanel.updateRoleNum();
                    }
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }// end function

        private function editSign(event:FocusEvent) : void
        {
            if (Cache.instance.role.entityInfo.level < 55)
            {
                this._roleSign.editable = false;
                MsgManager.showRollTipsMsg(Language.getString(41236));
                return;
            }
            this._roleSign.editable = true;
            if (this._roleSign.text == Language.getString(40004))
            {
                this._roleSign.text = "";
            }
            return;
        }// end function

        private function updateSign(event:FocusEvent) : void
        {
            if (Cache.instance.role.entityInfo.level < 55)
            {
                this._roleSign.editable = false;
                return;
            }
            this._roleSign.editable = true;
            if (this._roleSign.text != this._selfSign)
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.SignatureUpdate, this._roleSign.text));
            }
            this._selfSign = this._roleSign.text;
            if (this._roleSign.text == "")
            {
                this._roleSign.text = Language.getString(40004);
            }
            return;
        }// end function

        public function get friendSetPanel() : FriendSetPanel
        {
            return this._friendSetPanel;
        }// end function

        public function get friendDeletePanel() : FriendDeletePanel
        {
            return this._friendDeletePanel;
        }// end function

        public function get currSelGroup() : int
        {
            return this._currSelGroup;
        }// end function

        public function get tabBar() : GTabarNew
        {
            return this._tabBar;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            super.disposeImpl(param1);
            this._middlePaneBg.dispose(param1);
            this._roleLevel.dispose(param1);
            this._roleCareer.dispose(param1);
            this._roleName.dispose(param1);
            this._vipImg.dispose(param1);
            this._roleSign.dispose(param1);
            this._tabBar.dispose(param1);
            this._groupInfoSpr.dispose(param1);
            this._groupInfoSpr2.dispose(param1);
            this._roleListSpr.dispose(param1);
            this._findBtn.dispose(param1);
            this._deleteBtn.dispose(param1);
            this._friendSetPanel.dispose(param1);
            this._friendDeletePanel.dispose(param1);
            this._middlePaneBg = null;
            this._roleLevel = null;
            this._roleCareer = null;
            this._roleName = null;
            this._vipImg = null;
            this._roleSign = null;
            this._tabBar = null;
            this._groupInfoSpr = null;
            this._groupInfoSpr2 = null;
            this._roleListSpr = null;
            this._findBtn = null;
            this._deleteBtn = null;
            this._friendSetPanel = null;
            this._friendDeletePanel = null;
            this.removeListeners();
            return;
        }// end function

    }
}
